package com.deep.test.designpattern.singleton;

/**
 * <p>Description:
 * 单列：这种形式兼顾饿汉式的内存浪费，也兼顾synchronized性能问题</p>
 * Created with IDEA
 * author:hudepin
 * createTime:2018/5/17 10:35
 */
public class LazyLoadThreadSafe {
    private boolean initialized = false;
    private LazyLoadThreadSafe(){
        synchronized (this){
            if(!initialized){
                initialized = true;
            }else{
                throw new RuntimeException("singleton had attack");
            }
        }
    }

    private LazyLoadThreadSafe getInstance(){
        return LazyHolder.instance;
    }

    private static class LazyHolder {
        public static LazyLoadThreadSafe instance = new LazyLoadThreadSafe();
    }
}
